<?php
if(!defined('MY_ROOT'))
{
throw new Exception('Access Denied');
}
class member{
function register($user,$pass,$email,$exta=array())
{
$user=trim($user);
$pass=md5($pass);
if(!$this->check_avail($user))
{
return -1;
}
if(!check_email($email)){
return -2;
}
$exta=serialize($exta);
$cont['username']=$user;
$cont['password']=$pass;
$cont['email']=$email;
$cont['extra']=$exta;
return DB::Insert('member',$cont);
}
function check_avail($user)
{
$user=trim($user);
$cond="`username`=".DB::get_str($user);
$result=DB::select('member',$cond,'`id`',true);
if($result)
{
return false;
}else{
return true;
}
}
function login($user,$pass)
{
$user=trim($user);
$pass=md5($pass);
$cond="`username`=".DB::get_str($user)." AND `password` = ".DB::get_str($pass);
$result=DB::select('member',$cond,'`id`',true);
if(!$result)
{
return -1;
}
global $_G;
$_G['uid']=$result['id'];
$_G['username']=$result['username'];
$_G['usercfg']=unserialize($result['extra']);
$_SESSION['uid']=$_G['uid'];
$_SESSION['username']=$_G['username'];
$_SESSION['usercfg']=$result['extra'];
//$_SESSION['available']=$result['available'];
//$_G['available']=unserialize($result['available']);
return $result['uid'];
}
private function passwd($user,$pass)
{
  $pass=md5($pass);
  $user=trim($user);
  $con['password']=$pass;
  $where="`username`=".DB::get_str($user);
  DB::update('member',$con,$where);
}
function forgetpass($user)
{
$user=DB::get_str(trim($user));
$cond='`username`='.$user;
$result=DB::select('member',$cond,'`id`',true);
$time=getdate();
$hash=md5($result['username'].$time['yday'].$time['mon']);
$sid=SID;
MAIL::forgetpass($result['email'],$sid,$hash);
}
function chpasswd($user,$old,$new)
{
if($old==$new)
{return -1;}
$user=trim($user);
$old=md5($old);
$cond="`username`=".DB::get_str($user)." AND `password`=".DB::get_str($old);
$result=DB::select('member',$cond,'`id`',true);
if(!$result)
{
return -2;
}
$this->passwd($user,$new);
return 1;
}
function fpasswd($user,$hash,$new)
{
$time=getdate();
$user=trim($user);
$ha=md5($user.$time['yday'].$time['mon']);
if($ha==$hash)
{
$this->passwd($user,$new);
return 1;
}else{
return -1;
}
}
function update($extra=array())
{
global $_G;
$cond='`id`='.$_G['uid'];
$extra=DB::get_str(serialize($extra));
$set['extra']=$extra;
return DB::update('member',$set,$cond);
}
function logout()
{
session_destroy();
}
function activate($user)
{

}
}
?>
